package com.zky.gulimall.cart.vo;


import lombok.ToString;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description 购物车
 * @Author 张开源
 * @Date 2022/12/5 21:17
 * @Version 1.0
 **/
@ToString
public class Cart {

    /**
     * 购物车详细信息
     */
    private List<CartItem> items;

    /**
     * 商品数量
     */
    private Integer countNum;

    /**
     * 商品类型数量
     */
    private Integer countType;

    /**
     * 购物车商品总价
     */
    private BigDecimal totalAmount;
    /**
     * 减免价格
     */
    private BigDecimal reduce = new BigDecimal(0);

    public List<CartItem> getItems() {
        return items;
    }

    public Integer getCountNum() {
        int count = 0;
        if (!CollectionUtils.isEmpty(this.items)) {
            for (CartItem item : this.items) {
                count += item.getCount();
            }
        }
        return count;
    }

    public Integer getCountType() {
        int count = 0;
        if (!CollectionUtils.isEmpty(this.items)) {
            for (CartItem item : this.items) {
                count += 1;
            }
        }
        return count;
    }

    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal(0);
        if (!CollectionUtils.isEmpty(this.items)) {
            for (CartItem item : this.items) {
                if (item.getCheck()) {
                    amount = amount.add(item.getTotalPrice());
                }
            }
        }

        //减去优惠价
        BigDecimal result = amount.subtract(getReduce());
        return result;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }


    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
